<?php
/**
 * Created by PhpStorm.
 * User: manageistrator
 * Date: 2017/2/27
 * Time: 17:21
 */
namespace App\Http\Controllers\Manage;

use App\Http\Controllers\Manage\BaseController;
use App\Repositories\Manage\RoleRepo;
use App\Repositories\Manage\MenuRepo;
use Illuminate\Http\Request;
use App\Http\Requests;

class RoleController extends BaseController
{
    public $menuId = 1;

    public function roleList(roleRepo $roleRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'roleList');
        $data = $roleRepo->getRoleList();
        return view('manage.role.role_list', ['data' => $data]);
    }

    public function roleAdd()
    {
        $this->actionAuthorize($this->menuId, 'roleAdd');
        return view('manage.role.role_add ');
    }

    public function rolePostAdd(roleRepo $roleRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'roleAdd');
        $name = $request->get('name');
        $sort = $request->get('sort');
        $sort = empty($sort) ? '50' : $sort;
        try {
            $roleRepo->addRole($name, $sort);
            return $this->ajaxResponse(0, '添加成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '添加失败' . $e->getMessage());
        }

    }

    public function roleEdit(roleRepo $roleRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'roleEdit');
        $id = $request->get('id');
        $data = $roleRepo->getRole('id = ?', [$id]);
        return view('manage.role.role_edit', ['data' => $data]);
    }

    public function rolePostEdit(roleRepo $roleRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'roleEdit');
        $id = $request->get('id');
        $name = $request->get('name');
        $sort = $request->get('sort');
        $sort = empty($sort) ? '50' : $sort;
        try {
            $roleRepo->updaterole($id, $name, $sort);
            return $this->ajaxResponse(0, '修改成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '修改失败' . $e->getMessage());
        }

    }


    public function powerList(roleRepo $roleRepo, MenuRepo $menuRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'powerList');
        $roleId = $request->get('id');
        $roleData = $roleRepo->getRole('id = ?', [$roleId]);
        $roleMenuData = $roleRepo->getRoleMenuList('role_id = ?', [$roleId]);
        $menuData = $menuRepo->getMenuModule();
        return view('manage.role.power_list', ['name' => $roleData->name, 'roleMenuData' => $roleMenuData, 'menuData' => $menuData, 'roleId' => $roleId]);
    }


    public function powerPostAdd(roleRepo $roleRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'powerList');
        $powerStr = $request->get('power');
        $roleId = $request->get('roleId');
        $array = explode(',', $powerStr);
        foreach ($array as $key => $value) {
            $data = explode('_', $value);
            $arr[$key] = $data[0];
            $newArr[$key] = [$data[1] => $data[0]];
        }
        $arr = array_unique($arr);
        $menuIdData = $roleRepo->getMenuId('role_id = ?', [$roleId]);
        if (!empty($menuIdData)) {
            $diff = array_diff($menuIdData, $arr);
            foreach ($diff as $menuId) {
                $roleRepo->delRoleMenu('role_id = ? and menu_id = ?', [$roleId, $menuId]);
            }
        }

        try {
            foreach ($arr as $k => $val) {
                $num = 0;
                foreach ($newArr as $row => $res) {
                    if ($result = array_search($val, $res)) {
                        $num += $result;
                    }
                }
                $roleRepo->addRoleMenu($roleId, $val, $num);
            }
            return $this->ajaxResponse(0, '添加成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '添加失败' . $e->getMessage());
        }
    }


}